package com.moduls.data.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AtoboUtil {
	private static String url = "https://www.atobo.com.cn";
	private static String city_url = "https://www.atobo.com.cn/Companys/";
	private static String charset = "utf-8";
	private static HttpClientUtil httpClientUtil  = new HttpClientUtil();
	
	/**
	 * Atobo 城市数据
	 * @param httpClientUtil
	 * @param url
	 * @param charset
	 * @return
	 */
	public static List<Map<String, String>> atoboCity() {
        String result = httpClientUtil.doPost(city_url,charset);
		
		//查找位置 截取
		int first_1 = result.indexOf("<div class=\"filterArea\">");
		int first_2 = result.indexOf("<div><ul></ul></div></li></ul></div></div>");
		
		
		String city_strs=result.substring(first_1, first_2);

		List<Map<String, String>> list = new ArrayList<>();
		 int i=1;
		 String[] split_city_str=city_strs.split("<li><a href=\"");
		 for (String city_str : split_city_str) {
			if (i>=2) {
				 Map<String, String> map = new HashMap<>();
				 map.put("city_url", url+city_str.substring(0, city_str.indexOf("/\" title=")));
				 map.put("city_name", city_str.substring(city_str.indexOf("\">")+2, city_str.indexOf("</a>")));
				 list.add(map);
				 
				 if (i>5&&city_str.length() > 70&&!map.get("city_name").equals("彰化")) {//还有部分为截取出来 彰化是最后一个
					 Map<String, String> map2 = new HashMap<>();
					 int start = city_str.indexOf("<li class=\"title\"><a href=\"");
					 int length = "<li class=\"title\"><a href=\"".length();
					 String city_str_2 = city_str.substring(start+length,city_str.indexOf("</a>：</li><li class=\"alist\">"));
					 String url_2  = city_str_2.substring(0,city_str_2.indexOf("/\" title="));
					 map2.put("city_url", url+url_2);
					 //city_name 在找一次
					 map2.put("city_name", city_str_2.substring(city_str_2.indexOf("\">")+2));
					 list.add(map2);
				}
			}
			i+=1;
		}
		return list;
	}
	
	/**
	 * Atobo 城市公司地址
	 * @return
	 */
	public static List<String> atoboCityCompany(String urls) {
		//根据城市 一页页找城市的公司  Atobo 固定 每个城市下有50页公司
		List<String> list_company_url = new ArrayList<>();
		//每个城市下都是50页
		String city_company_url ="";
		for (int i = 1; i < 50; i++) {
			if (i>1) {
				city_company_url = urls+"-y"+i;//其他页数
			} else {
				city_company_url = urls;//第一页
			}
			
            String result = httpClientUtil.doPost(city_company_url,charset);
			
			//查找位置 截取
			int first_1 = result.indexOf("<ul><li class=\"product_box\"><div><ul><li class=\"p_name\"><div><ul>");
			int first_2 = result.indexOf("</a></li></ul></div></li></ul></div></li></ul></div>");
			
			String city_strs=result.substring(first_1, first_2);

			String[] split_city_str=city_strs.split("<li class=\"pp_name\"><a href=\"");
			int a = 1;
			for (String str : split_city_str) {
				if (a>=2) {
					String company_url = str.substring(0, str.indexOf("\" target=\"_blank\" class=\"CompanyName\""));
					if (company_url.indexOf("Companys")!=-1) {
						list_company_url.add(url+company_url);
					}
					
				}
				a+=1;
			}
			
		}
		
		return list_company_url;
	}
	
	/**
	 * atobo 公司详细信息
	 * @param urls
	 * @return
	 */
	public static Map<String, String> atoboCityCompanyInfo(String urls) {

		Map<String, String> map = new HashMap<>();
		
		String result = httpClientUtil.doPost(urls,charset);//公司页面数据
		
		System.out.println(result);
		
		//查找位置 截取
		/*int first_1 = 0;
		first_1 = result.indexOf("<div><table><tr><th>公司名称：</th><td>")+"<div><table><tr><th>公司名称：</th><td>".length();
		if (first_1 == -1) {
			first_1 = result.indexOf("<div><table><tr><th>店铺名称：</th><td>")+"<div><table><tr><th>店铺名称：</th><td>".length();
		}
		int first_2 = result.indexOf("</a></td></tr></table></div></div><div class=\"company-intro\">");
		
		String company_strs=result.substring(first_1, first_2);*/
		
		/*String company = company_strs.substring(0,company_strs.indexOf("</td></tr><tr><th>地址："));
		
		int address_start = company_strs.indexOf("</a> &nbsp;")+"</a> &nbsp;".length();
		int address_end = company_strs.indexOf("</td></tr><tr><th>邮编：");
		String address = company_strs.substring(address_start,address_end);
		
		int zip_code_start = company_strs.indexOf("邮编：</th><td>")+"邮编：</th><td>".length();
		int zip_code_end = company_strs.indexOf("</td></tr><tr><th>所在行业：");
		String zip_code = company_strs.substring(zip_code_start, zip_code_end);
		
		int user_name_start = company_strs.indexOf("<tr><th>联系人：</th><td>")+"<tr><th>联系人：</th><td>".length();
		int user_name_end = company_strs.indexOf("</td></tr><tr><th>电话：");
		String user_name = company_strs.substring(user_name_start, user_name_end);
		
		int telephone_start = company_strs.indexOf("电话：</th><td><img src=\"")+"电话：</th><td><img src=\"".length();
		int telephone_end = company_strs.indexOf("\" border=\"0\" align=\"absmiddle\" alt=\"\"/></td></tr><tr><th>手机：");
		String telephone = company_strs.substring(telephone_start, telephone_end);
		
		int phone_start = company_strs.indexOf("手机：</th><td><img src=\"")+"手机：</th><td><img src=\"".length();
		int phone_end = company_strs.indexOf("\"border=\"0\" align=\"absmiddle\" alt=\"\"/></td></tr><tr><th>传真：");
		String phone = company_strs.substring(phone_start, phone_end);*/
		
		//int website_start = company_strs.indexOf("html\">")+"html\">".length();
		//int website_end = company_strs.indexOf("</a></td></tr></table>");
		//String website = company_strs.substring(website_start, website_end);
		
		/*map.put("company", company);
		map.put("address", address);
		map.put("zip_code", zip_code);
		map.put("user_name", user_name);
		map.put("telephone", telephone);
		map.put("phone", phone);
		map.put("website", urls);*/
		map.put("result", result);
		
		return map;
	}
	
	
}
